package com.lindroy.androidplayer.viewmodels

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.constant.REPOSITORY_URL
import com.lindroy.androidplayer.logic.model.LoginModel
import com.lindroy.androidplayer.ui.widget.SettingBar
import com.lindroy.androidplayer.utils.SingleLiveEvent
import com.lindroy.androidplayer.utils.isLogin
import com.lindroy.androidplayer.utils.kvLoginInfo
import com.lindroy.androidplayer.utils.mapSingle
import com.lindroy.lib_utils.extensions.getResString

/**
 * @author Lin
 * @date 2022/2/10
 * @function
 */
class MineViewModel : ViewModel() {

    private val loginClickAction = SingleLiveEvent<Unit>()

    private val coinItemClickAction = SingleLiveEvent<Unit>()

    private val collectionClickAction = SingleLiveEvent<Unit>()

    private val settingClickAction = SingleLiveEvent<Unit>()

    private val shareItemClickAction = SingleLiveEvent<Unit>()

    private val loginInfoLiveData = SingleLiveEvent<LoginModel>()

    private val urlItemClickAction = SingleLiveEvent<Unit>()

    private val feedbackItemClickAction = SingleLiveEvent<Unit>()

    val nickName = loginInfoLiveData.mapSingle {
        it.nickname.takeIf { name -> name?.isNotBlank() == true } ?: getResString(
            R.string.login_need
        )
    }

    val loginClickLiveData = loginClickAction.mapSingle { }

    val coinItemClickLiveData = coinItemClickAction.mapSingle { }

    val shareItemClickLiveData = shareItemClickAction.mapSingle { }

    val collectionClickLiveData = collectionClickAction.mapSingle { }

    val settingClickLiveData = settingClickAction.mapSingle {  }

    val urlItemClickLiveData = urlItemClickAction.mapSingle { REPOSITORY_URL }

    val feedbackItemClickLiveData = feedbackItemClickAction.mapSingle {  }

    val coinItemClickListener = SettingBar.OnItemClickListener {
        coinItemClickAction.postValue(Unit)
    }

    val collectItemClickListener = SettingBar.OnItemClickListener {
        collectionClickAction.postValue(Unit)
    }

    val settingItemClickListener = SettingBar.OnItemClickListener {
        settingClickAction.postValue(Unit)
    }

    val shareItemClickListener = SettingBar.OnItemClickListener {
        shareItemClickAction.postValue(Unit)
    }

    val urlItemClickListener = SettingBar.OnItemClickListener {
        urlItemClickAction.postValue(Unit)
    }

    val feedbackItemClickListener = SettingBar.OnItemClickListener {
        feedbackItemClickAction.postValue(Unit)
    }

    fun setLoginInfo(loginModel: LoginModel?) {
        loginInfoLiveData.postValue(loginModel ?: LoginModel())
    }

    fun onLoginClick() {
        if (isLogin) {
            return
        }
        loginClickAction.postValue(Unit)
    }

}